package com.cy.personer.web;
import com.cy.personer.bean.Detail;
import com.cy.personer.bean.UploadResult;
import com.cy.personer.biz.DetailBiz;
import com.cy.personer.util.ImportExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.List;

/**
 * 
 *
 */

@Controller
@RequestMapping("/detail")
public class DetailHandler {
    @Resource
    DetailBiz detailBizImpl;

    /**
     * 以项目ID查询细项列表
     * @author peipei
     */
    @RequestMapping("findItemdetails.action")
    public ModelAndView findItemdetails(Detail detail, String pageSize, String pageNum){
        PageInfo<Detail> DetailPage=detailBizImpl.findItemdetails(detail,pageSize,pageNum);
        ModelAndView mov=new ModelAndView();
        mov.addObject("DetailPage",DetailPage);
        mov.setViewName("admin/detail_list");
        return mov;
    }
    /**
     * 以细项ID删除细项
     * @author peipei
     */
    @RequestMapping("/delDetail.action")
    public String delDetail(int detailId,int itemId){
        detailBizImpl.delDetail(detailId);
       return "redirect:/detail/findItemdetails.action?itemId="+itemId;
    }
    /**
     * 添加细项
     * @author peipei
     */
    @RequestMapping( "/addDetail.action")
    public String addDetail(Detail detail){
        int result=detailBizImpl.addDetail(detail);
        String msg=null;
        if(result>0){
            msg= "success";
        }else{
            msg= "error";
        }
        return "redirect:/detail/findItemdetails.action?itemId="+detail.getItemId()+"";
    }
    /**
     * 修改细项
     * @author peipei
     */
    @RequestMapping("/detail_edit.action")
    public @ResponseBody String editDetail(Detail detail){
        String msg=null;
        int result=detailBizImpl.editDetail(detail);
        if(result>0){
            msg= "success";
        }else{
            msg= "error";
        }
        return msg;
    }

    /**
     * 描述：通过传统方式form表单提交方式导入excel文件
     * @param request
     * @throws Exception
     */
    @RequestMapping(value="upload.action",method={RequestMethod.GET,RequestMethod.POST} )
    public @ResponseBody UploadResult uploadExcel(HttpServletRequest request) throws Exception {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        InputStream in =null;
        List<List<Object>> listob = null;
        MultipartFile file = multipartRequest.getFile("upfile");
        if(file.isEmpty()){
            throw new Exception("文件不存在！");
        }
        in = file.getInputStream();
        listob = new ImportExcelUtil().getBankListByExcel(in,file.getOriginalFilename());
        in.close();
        UploadResult uploadResult=detailBizImpl.addDetails(listob);
        /*for (int i = 0; i < listob.size(); i++) {
            List<Object> lo = listob.get(i);
           int itemId=detailBizImpl.findItemIdByItemName(String.valueOf(lo.get(1))) ;
            Detail detail = new Detail();
            detail.setDetailName(String.valueOf(lo.get(0)));
            detail.setItemId(itemId);
            detail.setUnit(String.valueOf(lo.get(2)));
            detail.setMin(Integer.parseInt(String.valueOf(lo.get(3))));
            detail.setMax(Integer.parseInt(String.valueOf(lo.get(4))));
            detail.setSerialId(Integer.parseInt(String.valueOf(lo.get(5))));
            System.out.println("打印信息-->项目名:"+detail.getDetailName()+"  项目ID："+detail.getItemId()+"   最小："+detail.getMin()+"" +
                    "   最大："+detail.getMax()+ "单位："+detail.getUnit() );
        }*/
        return uploadResult;
    }

}
